package com.fengziyu.lib_recyclerview_fzy.goods

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.fengziyu.lib_recyclerview_fzy.entity.GoodsResponse
import com.fengziyu.lib_recyclerview_fzy.net.ApiService
import com.fengziyu.lib_recyclerview_fzy.net.RetrofitUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class GoodsRepository {

    private val apiService: ApiService by lazy { RetrofitUtil.getApiService() }

    fun getGoods(categoryId: Int , currentPage: Int , pageSize: Int): LiveData<GoodsResponse>{
        val goodsResponse = MutableLiveData<GoodsResponse>()
        apiService.getGoods(categoryId , currentPage , pageSize).enqueue(object : Callback<GoodsResponse>{
            override fun onResponse(call: Call<GoodsResponse>, response: Response<GoodsResponse>) {
                if(response.isSuccessful){
                    goodsResponse.value = response.body()
                }else{
                    Log.i("TAGA", "onResponse: 出错了${response.message()}")
                }
            }

            override fun onFailure(call: Call<GoodsResponse>, t: Throwable) {
                Log.i("TAGA", "onResponse: 出错了${t.message}")
            }
        })
        return goodsResponse
    }

}